<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="right-bar">
        <a-button
          @click="edit(0)"
          class="nbcs-btn nbcs-btn-primary position-r"
          style="top:-1.6px"
          >新增敏感词类型</a-button>
      </template>
    </filter-bar>
    <a-table
      ref="_table"
      :columns="columns"
      :data-source="list"
      :pagination="pageConfig"
      rowKey="id"
      :loading="loading"
      :scroll="{ x: 1000, y: tableHeight }"
    >
      <template slot="roles" slot-scope="text">
        <a-tag v-for="(item,index) in text" :key="index">{{item.name}}</a-tag>
      </template>
      <span slot="tags" slot-scope="text, record">
        <a-tag v-if="record.enabled" class="tag-green">
          正常
        </a-tag>
        <a-tag v-else class="tag-red">
          锁定
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="edit(1, record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm title="确定删除该敏感词吗？" @confirm="del(record)">
          <a-icon slot="icon" type="question-circle-o" style="color: red" />
          <a class="table-red-btn">删除</a>
        </a-popconfirm>
      </span>
    </a-table>
    <word-form ref="wordForm" @getList="getList(page, searchForm)"></word-form>
  </div>
</template>

<script>
import FilterBar from "@/layouts/FilterBar.vue";
import { itemPage, delitem} from "@/services/word";
import WordForm from "./wordTpyeForm.vue";
import {cutDateSecond} from '@/utils/date'
export default {
  name: "WordType",
  components: { FilterBar, WordForm },
  data() {
    return {
      tableHeight: 0,
      loading: false,
      searchForm: {
          type:"sensitive"
      },
      list: [],
      columns: [
        { title: "敏感词类型",dataIndex: "label",width: 550, },
        // {
        //   title: "敏感类型",
        //   dataIndex: "wordType",
        // },
        // {
        //   title: "词库类型",
        //   dataIndex: "sensitiveType",
        // },
        {
          title: "创建时间",
          dataIndex: "createTime",
          customRender:text=>{
            return cutDateSecond(text)
          }},
        {
          title: "操作",
          key: "operation",
          fixed: "right",
          width: 200,
          scopedSlots: { customRender: "action" }
        }
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultPageSize: 20,
        pageSizeOptions: ["20", "30", "50"],
        total: 0,
        current:1,
        showTotal: total => {
          return `共 ${total} 条`;
        },
        onShowSizeChange: (page, pageSize) => {
          this.page.currentPage = 1;
          this.page.pageSize = pageSize;
          this.getList(this.page, this.searchForm);
        },
        onChange: (page) => {
          this.page.currentPage = page;
          this.getList(this.page, this.searchForm);
        },
      },
      page: {
        currentPage: 1,
        pageSize: 20
      },
      type:"sensitive"
    };
  },
  computed: {},
  created() {
    console.log(1);
    this.getList(this.page);
  },
  mounted() {
    this.tableHeight = this.$refs._table.$el.clientHeight - 100;
  },
  methods: {
    getList(page, params) {
      this.loading = true;
      itemPage(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize,
            type:this.type
          },
          params
        )
      ).then(res => {
        this.loading = false;
        const { data } = res.data;
        this.list = data.list;
        this.pageConfig.current = data.page;
        this.pageConfig.total = data.totalRow;
      });
    },
    del(obj) {
      delitem({ id: obj.id }).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功");
          this.getList(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //添加/编辑
    edit(action, data = {}){
      this.$refs['wordForm'].changeForm(true,action,data);
    },
  }
};
</script>

<style lang="less" scoped>
.select {
  width: 170px;
  /deep/ .ant-select-selection {
    width: 100%;
  }
}
._input {
  width: 180px;
}
.table-page {
  .ant-table-wrapper {
    height: calc(100% - 70px);
  }
}
</style>
